#!/usr/bin/python
# -*- coding: UTF-8 -*-
from selenium import webdriver
from requests import Session
from time import sleep
# import re
import requests
import sys
import datetime    #调用事件模块
import json

today = datetime.date.today()
ISOFORMAT='%Y%m%d' #设置输出格式
days=today.strftime(ISOFORMAT)
print days

req = Session()
req.headers.clear() 
wd = webdriver.Chrome() 
zhihuLogInUrl = 'http://portal.grn.cn/html/nds/portal/portal.jsp'
wd.get(zhihuLogInUrl)

wd.find_element_by_id('login').send_keys('zhanghongyan@grn.cn') 
wd.find_element_by_id('password1').send_keys('8888')
sleep(10) #手动输入验证码 
wd.find_element_by_id('login-submit').submit() 
sleep(10)#等待Cookies加载
cookies = wd.get_cookies()
for cookie in cookies:
    req.cookies.set(cookie['name'],cookie['value'])
    print(cookie['name'],cookie['value'])

# cookies = '''COOKIE_SUPPORT=true;LOGIN=zhanghongyan@grn.cn;GUEST_LANGUAGE_ID=zh_CN;AD_GRN_portal=48043037;JSESSIONID=gvp2y1c9bt6t'''

headers = {    
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',    
'Connection': 'keep-alive',     
'Cache-Control': "no-cache",  
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
# ,'Cookie': cookies
}

url = "http://portal.grn.cn/servlets/dwr/call/plaincall/Controller.query.dwr"

payload = "callCount=1\npage=/html/nds/portal/ssv/index.jsp?ss=13\nhttpSessionId=gvp2y1c9bt6t\nscriptSessionId=yfzhu642\nc0-scriptName=Controller\nc0-methodName=query\nc0-id=0\nc0-param0=string:%7B%22init_query%22%3A%20false%2C%20%22range%22%3A%2020%2C%20%22show_alert%22%3A%20true%2C%20%22start%22%3A%200%2C%20%22qlcid%22%3A%201047512%2C%20%22dir_perm%22%3A%201%2C%20%22fixedcolumns%22%3A%20%22%22%2C%20%22orders%22%3A%20%5B%7B%22d%22%3A%20%22%E6%8E%92%E5%90%8D%22%2C%20%22c%22%3A%20%22V_STORERETAILORDERDAY.ORDERNO%22%7D%5D%2C%20%22table%22%3A%20%22V_STORERETAILORDERDAY%22%2C%20%22callbackEvent%22%3A%20%22RefreshGrid%22%2C%20%22subtotal%22%3A%20true%2C%20%22param_str2%22%3A%20%22table%3D99922374%26tab_count%3D1%26return_type%3Dn%26accepter_id%3Dnull%26qlcid%3D1047512%26param_count%3D3%26resulthandler%3D%252Fhtml%252Fnds%252Fportal%252Ftable_list.jsp%26show_maintableid%3Dtrue%26V_STORERETAILORDERDAY.DATEDESC%3D"+days+"~"+days+"%26V_STORERETAILORDERDAY.DATEDESC_1%3D"+days+"%26V_STORERETAILORDERDAY.DATEDESC_2%3D"+days+"%26V_STORERETAILORDERDAY.C_STORE_ID%3D(((%25E5%25BA%2597%25E4%25BB%2593%25E5%25B1%259E%25E6%2580%25A7%25E6%25BB%25A1%25E8%25B6%25B3%25E5%258C%2585%25E5%2590%25AB(%25E7%259B%25B4%25E8%2590%25A5%252C%25E8%2581%2594%25E8%2590%25A5))%2520%25E5%25B9%25B6%25E4%25B8%2594%2520%2520(%25E5%258F%25AF%25E7%2594%25A8%2520%253D%2520%25E6%2598%25AF)%2520)%2520%25E5%25B9%25B6%25E4%25B8%2594%2520(%2520(%25E4%25B8%2580%25E7%25BA%25A7%25E7%25BB%258F%25E9%2594%2580%25E5%2595%2586%25E5%2590%258D%25E7%25A7%25B0%2520%253D%2520%253D%25E6%25A0%25AA%25E6%25B4%25B2)%2520%2520%25E5%25B9%25B6%25E4%25B8%2594%2520%2520(%25E5%258F%25AF%25E7%2594%25A8%2520%253D%2520%25E6%2598%25AF)%2520))%26V_STORERETAILORDERDAY.C_STORE_ID%252Fsql%3DIN(SELECT%2520C_STORE.ID%2520FROM%2520C_STORE%252CC_BLOCK%2520a0%2520WHERE%2520(%2520(C_STORE.AD_CLIENT_ID%253D37)%2520)%2520AND%2520(a0.ID%2520(%252B)%253DC_STORE.C_BLOCK_ID)%2520AND%2520(%2520(%2520(%2520(C_STORE.C_STOREATTRIB1_ID%2520IN(SELECT%2520C_STOREATTRIBVALUE.ID%2520FROM%2520C_STOREATTRIBVALUE%2520WHERE%2520(%2520(C_STOREATTRIBVALUE.AD_CLIENT_ID%253D37)%2520)%2520AND%2520(%2520(C_STOREATTRIBVALUE.ID%2520IN(310%252C311))%2520)))%2520%2520AND%2520%2520(C_STORE.ISACTIVE%2520%253D%2520'Y')%2520)%2520%2520AND%2520%2520(%2520(a0.NAME%2520%253D%2520'%25E6%25A0%25AA%25E6%25B4%25B2')%2520%2520AND%2520%2520(C_STORE.ISACTIVE%2520%253D%2520'Y')%2520)%2520)%2520))%26V_STORERETAILORDERDAY.C_STORE_ID%252Ffilter%3D%253Cfilter%253E%253Cdesc%253E(((%25E5%25BA%2597%25E4%25BB%2593%25E5%25B1%259E%25E6%2580%25A7%25E6%25BB%25A1%25E8%25B6%25B3%25E5%258C%2585%25E5%2590%25AB(%25E7%259B%25B4%25E8%2590%25A5%252C%25E8%2581%2594%25E8%2590%25A5))%2520%25E5%25B9%25B6%25E4%25B8%2594%2520%2520(%25E5%258F%25AF%25E7%2594%25A8%2520%253D%2520%25E6%2598%25AF)%2520)%2520%25E5%25B9%25B6%25E4%25B8%2594%2520(%2520(%25E4%25B8%2580%25E7%25BA%25A7%25E7%25BB%258F%25E9%2594%2580%25E5%2595%2586%25E5%2590%258D%25E7%25A7%25B0%2520%253D%2520%253D%25E6%25A0%25AA%25E6%25B4%25B2)%2520%2520%25E5%25B9%25B6%25E4%25B8%2594%2520%2520(%25E5%258F%25AF%25E7%2594%25A8%2520%253D%2520%25E6%2598%25AF)%2520))%253C%252Fdesc%253E%253Csql%253EIN(SELECT%2520C_STORE.ID%2520FROM%2520C_STORE%252CC_BLOCK%2520a0%2520WHERE%2520(%2520(C_STORE.AD_CLIENT_ID%253D37)%2520)%2520AND%2520(a0.ID%2520(%252B)%253DC_STORE.C_BLOCK_ID)%2520AND%2520(%2520(%2520(%2520(C_STORE.C_STOREATTRIB1_ID%2520IN(SELECT%2520C_STOREATTRIBVALUE.ID%2520FROM%2520C_STOREATTRIBVALUE%2520WHERE%2520(%2520(C_STOREATTRIBVALUE.AD_CLIENT_ID%253D37)%2520)%2520AND%2520(%2520(C_STOREATTRIBVALUE.ID%2520IN(310%252C311))%2520)))%2520%2520AND%2520%2520(C_STORE.ISACTIVE%2520%253D%2520%2526apos%253BY%2526apos%253B)%2520)%2520%2520AND%2520%2520(%2520(a0.NAME%2520%253D%2520%2526apos%253B%25E6%25A0%25AA%25E6%25B4%25B2%2526apos%253B)%2520%2520AND%2520%2520(C_STORE.ISACTIVE%2520%253D%2520%2526apos%253BY%2526apos%253B)%2520)%2520)%2520))%253C%252Fsql%253E%253Cexpr%253E%2526lt%253B!%255BCDATA%255B%2526lt%253Bexpr%2526gt%253B%2526lt%253Bexpr%2526gt%253B%2526lt%253Bexpr%2526gt%253B%2526lt%253Bdesc%2526gt%253B(%25E5%25BA%2597%25E4%25BB%2593%25E5%25B1%259E%25E6%2580%25A7%25E6%25BB%25A1%25E8%25B6%25B3%25E5%258C%2585%25E5%2590%25AB(%25E7%259B%25B4%25E8%2590%25A5%252C%25E8%2581%2594%25E8%2590%25A5))%2526lt%253B%252Fdesc%2526gt%253B%2526lt%253Bclink%2526gt%253BC_STORE.C_STOREATTRIB1_ID%2526lt%253B%252Fclink%2526gt%253B%2526lt%253Bcondition%2526gt%253BIN(SELECT%2520C_STOREATTRIBVALUE.ID%2520FROM%2520C_STOREATTRIBVALUE%2520WHERE%2520(%2520(C_STOREATTRIBVALUE.AD_CLIENT_ID%253D37)%2520)%2520AND%2520(%2520(C_STOREATTRIBVALUE.ID%2520IN(310%252C311))%2520))%2526lt%253B%252Fcondition%2526gt%253B%2526lt%253B%252Fexpr%2526gt%253B%2526lt%253Boper%2526gt%253B1%2526lt%253B%252Foper%2526gt%253B%2526lt%253Bexpr%2526gt%253B%2526lt%253Bclink%2526gt%253BC_STORE.ISACTIVE%2526lt%253B%252Fclink%2526gt%253B%2526lt%253Bcondition%2526gt%253B%253DY%2526lt%253B%252Fcondition%2526gt%253B%2526lt%253B%252Fexpr%2526gt%253B%2526lt%253B%252Fexpr%2526gt%253B%2526lt%253Boper%2526gt%253B1%2526lt%253B%252Foper%2526gt%253B%2526lt%253Bexpr%2526gt%253B%2526lt%253Bexpr%2526gt%253B%2526lt%253Bclink%2526gt%253BC_STORE.C_BLOCK_ID%253BNAME%2526lt%253B%252Fclink%2526gt%253B%2526lt%253Bcondition%2526gt%253B%253D%25E6%25A0%25AA%25E6%25B4%25B2%2526lt%253B%252Fcondition%2526gt%253B%2526lt%253B%252Fexpr%2526gt%253B%2526lt%253Boper%2526gt%253B1%2526lt%253B%252Foper%2526gt%253B%2526lt%253Bexpr%2526gt%253B%2526lt%253Bclink%2526gt%253BC_STORE.ISACTIVE%2526lt%253B%252Fclink%2526gt%253B%2526lt%253Bcondition%2526gt%253B%253DY%2526lt%253B%252Fcondition%2526gt%253B%2526lt%253B%252Fexpr%2526gt%253B%2526lt%253B%252Fexpr%2526gt%253B%2526lt%253B%252Fexpr%2526gt%253B%255D%255D%2526gt%253B%253C%252Fexpr%253E%253C%252Ffilter%253E%26V_STORERETAILORDERDAY.ORDERNO%3D%26show_all%3Dtrue%26queryindex_-1%3D-1%22%2C%20%22resulthandler%22%3A%20%22%2Fhtml%2Fnds%2Fportal%2Ftable_result.jsp%22%2C%20%22totalRowCount%22%3A%203%7D\nbatchId=0"

response = req.request("POST", url, data=payload, headers=headers)
lens = len(response.text)
ret=response.text[71:lens-5]
print(ret)

# data = json.loads(ret)

# print data.subtotalRow